<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\BaseController;
use App\Models\Card;

/***
 * Class AdminController
 * @package App\Http\Controllers\Admin
 * 后台管理员
 */
class CardController extends BaseController
{

    public function __construct(Card $card)
    {
        parent::__construct();
        $this->card = $card;
    }

    /***
     * 管理员列表
     */
    public function getIndex()
    {
        $keyword = request('keyword', '');
        $where = [];
        if ($keyword) {
            if (is_numeric($keyword)) {
                $where['id'] = $keyword;
            } else {
                $where['nickname'] = ['like', '%' . $keyword . '%'];
            }
        }

        $order['id'] = 'desc';
        $rows = $this->card->getList($where, '*', $order, 5);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'],request('page',1),10);
        }
        return view('admin.group.index', compact('rows', 'keyword'));
    }

    /***
     * 购买房卡
     */
    public function getBuy()
    {
        return view('admin.group.add');
    }

    public function postBuy()
    {
        $res = $this->card->add(request());
        return ajax_return($res);
    }

    /***
     * 出售房卡
     */
    public function getSale()
    {
        return view('admin.group.add');
    }

    public function postSale()
    {
        $res = $this->card->add(request());
        return ajax_return($res);
    }

    public function getModify()
    {
        $field = request('field');
        $ids = trim(trim(request('ids')), ',');
        $where['id'] = 1;
        $data[$field] = $ids;
        $res = $this->indexList->up($where, $data);
        $result['status'] = $res;
        $result['message'] = '操作成功';
        if (!$res) {
            $result['message'] = '操作失败';
        }
        return response()->json($result);
    }

    /***
     * 编辑
     */
    public function getEdit($id)
    {
        $admins = $this->model->getOne(['id' => $id]);
        return view('admin.group.edit', compact('admins'));
    }

    public function postEdit($id)
    {
        $data = request();
        $res = $this->model->update($id, $data);
        if ($res) {
            return redirect()->action(NAMESPACE_ADMIN . 'AdminController@getIndex');
        }
        return redirect()->action(NAMESPACE_ADMIN . 'AdminController@getEdit', ['id' => $id]);
    }

    /***
     * @param $id
     * 删除用户
     */
    public function getDel($id)
    {
        $all_data = request();
        $res = $this->model->del($id);
        if ($res) {
            return redirect()->action(NAMESPACE_ADMIN . 'AdminController@getIndex');
        }
    }

    /**
     * 更新用户状态
     */
    public function getChange()
    {
        $id = request('id');
        $status = request('status');
        $res = $this->model->up($id, ['status' => $status]);
        if ($res) {
            echo json_encode(['code' => 200, 'msg' => 'success']);
            exit;
        }
        echo json_encode(['code' => 500, 'msg' => 'fail']);
    }
}